package com.cqjtu.checkbook;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by Administrator on 2016/10/29 0029.
 */
public class MyDBHelper extends SQLiteOpenHelper  {
    public static final String CREATE_TABLE = "create table CheckBook ("
            + "id integer primary key autoincrement, "
            + "Sno text, "
            + "Sname text, "
            + "Sclass text, "
            + "State text, "
            + "img blob, "
            + "zaiqin integer default (0), "
            + "chidao integer default (0), "
            + "zaotui integer default (0), "
            + "kuangke integer default (0), "
            + "bingjia integer default (0))";


    private Context context;
    public MyDBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(CREATE_TABLE);
        readFileByLines("check.txt",sqLiteDatabase);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
    /**
     * 以行为单位读取文件，常用于读面向行的格式化文件
     */
    public static void readFileByLines(String fileName,SQLiteDatabase sqLiteDatabase) {
        BufferedReader reader = null;
        try {

            InputStreamReader inputReader = new InputStreamReader( MyApplication.getContext()
                    .getResources().getAssets().open(fileName) );
             reader = new BufferedReader(inputReader);
            System.out.println("以行为单位读取文件内容，一次读一整行：");
            String tempString = null;
            //int line = 1;
            // 一次读入一行，直到读入null为文件结束
            Bitmap  bmp = BitmapFactory.decodeResource(MyApplication.getContext().getResources(), R.drawable.admin);
            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
            while ((tempString = reader.readLine()) != null) {

                String[] str = tempString.split(" ");
                ContentValues values = new ContentValues();
                values.put("id",Integer.valueOf(str[0]));
                values.put("Sno",str[1]);
                values.put("Sname",str[2]);
                values.put("Sclass",str[3]);
                //默认 在勤
                values.put("State","在勤");
                values.put("img",baos.toByteArray());
                sqLiteDatabase.insert("CheckBook",null,values);
               // line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }
}
